"""
Case Type   : desc
Case Name   : describe分析临时表结构
Create At   : 2024.09.23
Owner       : @zhanghuan96
Description :
    1、建全局临时表插入数据进行describe
    2、建本地临时表插入数据并进行describe
    3、清理环境
Expect      :
    1、成功，插件查询成功
    2、成功
    3、成功
History     :
"""

import os
import unittest

import py_opengauss
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class CompatibilityTest01(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.user_node = Node('PrimaryDbBmode')
        self.primary_sh = CommonSH('PrimaryDbBmode')
        self.constant = Constant()
        self.tb_name = 'tb_desc_0009'
        self.con_info = f'opengauss://' \
                        f'{self.user_node.db_user}:' \
                        f'{self.user_node.db_password}@' \
                        f'{self.user_node.db_host}:' \
                        f'{self.user_node.db_port}/' \
                        f'{self.user_node.db_name}'

    def test_create_function(self):
        text = '-----step1:建全局临时表插入数据进行desc;expect:成功-----'
        self.log.info(text)
        sql_cmd2 = (f"drop table if exists {self.tb_name};"
                    f"create table {self.tb_name}(c_int int,c_text text);;"
                    f"insert into {self.tb_name} values(1,'test'),(2,'test1');")
        self.log.info(sql_cmd2)
        sql_res2 = self.primary_sh.execut_db_sql(sql_cmd2)
        self.log.info(sql_res2)
        self.assertIn('CREATE TABLE', sql_res2, '创建失败')
        self.assertIn('INSERT 0 2', sql_res2, '创建失败')

        text = '-step2:建本地临时表插入数据并进行describe;expect:成功-'
        self.log.info(text)
        self.db1 = py_opengauss.open(self.con_info)
        ddl_sql = f'''drop table if exists {self.tb_name}_01;'''
        result = self.db1.query(ddl_sql)
        self.log.info(result)
        self.assertIn('DROP TABLE', result)

        ddl_sql = f'''create temporary table {self.tb_name}_01 as select * \
        from {self.tb_name};'''
        result = self.db1.query(ddl_sql)
        self.log.info(result)
        self.assertIn('INSERT', result)

        ddl_sql = f'''select nspname from pg_namespace \
               where oid = (select relnamespace from pg_class where \
               relname = '{self.tb_name}_01');;'''
        result = self.db1.query(ddl_sql)
        self.log.info(result)
        query_res = result[0][0]
        self.log.info(query_res)

        ddl_sql = f'''describe {query_res}.{self.tb_name}_01;'''
        result = self.db1.query(ddl_sql)
        self.log.info(result)
        expect = [('c_int', 'integer', 'YES', None, 'NULL', ''),
                  ('c_text', 'text', 'YES', None, 'NULL', '')]
        self.assertEqual(result, expect, text)

    def tearDown(self):
        text = '-----step3:清理环境;expect:成功-----'
        self.log.info(text)
        self.db1.close()
        result = self.primary_sh.execut_db_sql(f'drop table if exists '
                                               f'{self.tb_name},'
                                               f'{self.tb_name}_01;')
        self.log.info(result)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS,
                      result, "执行失败:" + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
